#define DEF_MEASURE_ARITHMETIC_H
#include "measure_arithmetic.h"
#include <math.h>

void measureItemCalc(MeasureParam *measParam) {
    S32 ch;
    for (ch = CH1; ch < CH_TOTAL; ch++) {
        measureItemCalcOne(&measParam->measItem[ch]);
    }
}

void measureItemCalcOne(MeasureItem *measItem) {
    if (measItem->onoff == OFF) {
        return;
    }
    measureItemVrms(measItem);
    measureItemPauseVrms(measItem);
    measureItemVtopVbaseShoot(measItem);
    measureItemRiseFallTime(measItem);
    measureItemPulseWidth(measItem);
    measureItemFreqCycle(measItem);
}

void measureItemVrms(MeasureItem *measItem) {
    F64 squareSum = 0; //平方和
    F64 vrms = 0;
    S32 i;
    //计算均方根值和平均值
    for (i = 0; i < measItem->waveSize; i++) {
        squareSum = squareSum + (F64) ((F64) measItem->adc_without_zero[i] * (F64) measItem->adc_without_zero[i]);
    }
    if (measItem->waveSize > 0) {
        vrms = squareSum / (F64) measItem->waveSize;
        measItem->Vrms = sqrt(vrms);
    } else {
        measItem->Vrms = 0;
    }
}

void measureItemPauseVrms(MeasureItem *measItem) {
    F64 squareSum = 0; //平方和
    F64 pauseVrms = 0;
    S32 i;
    //计算第一个脉冲的均方根值和平均值
    for (i = measItem->riseP1; i < measItem->fallP1; i++) {
        squareSum = squareSum + (F64)((F64)measItem->adc_without_zero[i] * (F64)measItem->adc_without_zero[i]);
    }
    if (0 != (measItem->fallP1 - measItem->riseP1)) {
        pauseVrms = squareSum / ((F64)(measItem->fallP1 - measItem->riseP1));
        measItem->pauseRMS = sqrt(pauseVrms);
    }
    else {
        measItem->pauseRMS = 0;
    }
}


void measureItemVtopVbaseShoot(MeasureItem *measItem) {
    S32 i, maxmin;
    S32 winLow, winHigh; //中间值窗口，上下限
    S32 firstEdgePos = -1;
    S32 firstEdgeType; // 0--上升沿，1--下降沿。

    maxmin = (measItem->max + measItem->min) / 2;
    winHigh = maxmin + AMP_DATA_WINDOW;
    winLow = maxmin - AMP_DATA_WINDOW;
    measItem->Vtop = 0;
    measItem->Vbase = 0;

    firstEdgePos = findFristEdgePosition(measItem, winHigh, winLow, &firstEdgeType);

    if (firstEdgePos < 0) {
        measItem->Vtop = PvalueFail;
        measItem->Vbase = NvalueFail;
    } else {
        if (RISE == firstEdgeType) {
            findRisePosition(measItem, firstEdgePos, winHigh, winLow);
        } else {
            findFallPosition(measItem, firstEdgePos, winHigh, winLow);
        }

        if (0 == measItem->edgeBeing) {
            measItem->Vtop = PvalueFail;
            measItem->Vbase = NvalueFail;
        } else {
            //            if (SW_ON == arith_ch->antiphase) {
            //                arith_ch->ch_zero = -arith_ch->ch_zero;
            //            }
            getVtopVbaseShoot(measItem, firstEdgeType);
            getCycleRMS(measItem, firstEdgeType);
        }
    }
}

/**************************************************************************************************
 * 函数:        findFristEdgePosition
 * 说明:  获取遍历第一个边沿的起始位置。
 * 输入:  	
 *		S32 winHigh	: 窗口高值	
 *		S32 winLow	: 窗口低值
 *		S32 *edgeType	: 返回值--第一个边沿的类型，0:上升沿，1:下降沿。	
 * 关联变量: 
 * 返回: -1:遍历到终点未找到,>=0:遍历第一个边沿的起始位置。
 * 作者: 
 **************************************************************************************************/
S32 findFristEdgePosition(MeasureItem *measItem, S32 winHigh, S32 winLow, S32 *edgeType) {
    S32 i;
    S32 rise_pos = -1;
    S32 fall_pos = -1;
    S32 value_above_high = 0; // 值>=高值标志,0--<,1-->=
    S32 value_below_low = 0; // 值<=低值标志,0-->,1--<=
    S32 first_edge_type; // 0--上升沿，1--下降沿。
    S32 adc_value;

    //此处步进设为1，处理脉冲波占空比很小的情况(如50Hz,占空比为1%的波形)
    for (i = 0; i < measItem->waveSize; i++) {
        // 查找上升沿位置
        adc_value = measItem->adc_without_zero[i];
        if (adc_value <= winLow) {
            rise_pos = i;
        }
        if (rise_pos != -1 && value_above_high == 0 && adc_value >= winHigh) {
            value_above_high = 1;
            first_edge_type = RISE;
            break;
        }

        // 查找下降沿位置
        if (adc_value >= winHigh) {
            fall_pos = i;
        }
        if (fall_pos != -1 && value_below_low == 0 && adc_value <= winLow) {
            value_below_low = 1;
            first_edge_type = FALL;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        *edgeType = -1;
        return -1;
    }

    // 处理脉冲波占空比很小的情况(如50Hz,占空比为1%的波形)
    if (rise_pos > 1) {
        rise_pos -= 1;
    }
    if (fall_pos > 1) {
        fall_pos -= 1;
    }

    *edgeType = first_edge_type;
    if (first_edge_type == RISE) {
        return rise_pos;
    } else {
        return fall_pos;
    }
}

/******************************************************************
 * 函数名:findRisePosition 
 * 功能描述:
 * 获取上升沿位置和周期的点数
 * 输入: start--比较数据的起始位置，winHigh--窗口高电平，winLow--窗口低电平
 * 输出: 
 * 返回值: 上升沿的上升位置---即触发值加上半个窗口的位置
 * 
 * 作者:
 * 日期:
 * 备注:
 * -----------------------------------------
 *****************************************************************/
void findRisePosition(MeasureItem *measItem, S32 start, S32 winHigh, S32 winLow) {
    S32 i;
    S32 j = 0;
    S32 count_begin = 0;

    //计算正脉冲和负脉冲个数
    measItem->NPulseNum = 0;
    measItem->PPulseNum = 0;

    for (i = start; i < measItem->waveSize;) {
        if (j == 0) {
            for (; i < measItem->waveSize; i = i + measItem->stepValue) {
                if (winHigh < measItem->adc_without_zero[i]) {
                    if (count_begin != 0) {
                        measItem->NPulseNum++;
                    }
                    break;
                }
            }
            j = 1;
        } else {
            for (; i < measItem->waveSize; i = i + measItem->stepValue) {
                if (winLow > measItem->adc_without_zero[i]) {
                    measItem->PPulseNum++;
                    count_begin = 1;
                    break;
                }
            }
            j = 0;
        }
    }

    //查询第一个上升位置
    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHigh < measItem->adc_without_zero[i]) {
            measItem->riseP1 = i;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->riseP1 = EdgeFail;
        measItem->peridDataSum = EdgeFail;
        measItem->edgeBeing = 0;
        return;
    } else {
        measItem->edgeBeing = 1;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winLow > measItem->adc_without_zero[i]) {
            measItem->fallP1 = i;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->fallP1 = EdgeFail;
        measItem->peridDataSum = EdgeFail;
        return;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHigh < measItem->adc_without_zero[i]) {
            measItem->riseP2 = i;
            measItem->peridDataSum = measItem->riseP2 - measItem->riseP1;
            return;
        }
    }

    measItem->riseP2 = EdgeFail;
    measItem->peridDataSum = EdgeFail;
}

/******************************************************************
 * 函数名: findFallPosition 
 * 功能描述: - 
 *    获取下降沿位置和周期的点数
 * 输入: start--比较数据的起始位置，winHigh--窗口高电平，winLow--窗口低电平
 arith_ch--计算值的结构体，wavepro--数据处理的数据结构
 * 输出: 
 * 返回值: 下降沿的下降位置---即触发值加上半个窗口的位置
 * 
 * 作者:
 * 日期:
 * 备注:
 * -----------------------------------------
 *****************************************************************/

void findFallPosition(MeasureItem *measItem, S32 start, S32 winHigh, S32 winLow) {
    S32 i;
    S32 j;
    S32 count_begin = 0;
    j = 0;

    //计算上升边沿和个降边沿个数

    measItem->NPulseNum = 0;
    measItem->PPulseNum = 0;

    for (i = start; i < measItem->waveSize;) {
        if (j == 0) {
            for (; i < measItem->waveSize; i = i + measItem->stepValue) {
                if (winLow > measItem->adc_without_zero[i]) {
                    if (count_begin != 0) {
                        measItem->PPulseNum++;
                    }
                    break;
                }
            }
            j = 1;
        } else {
            for (; i < i < measItem->waveSize; i = i + measItem->stepValue) {
                if (winHigh < measItem->adc_without_zero[i]) {
                    measItem->NPulseNum++;
                    count_begin = 1;
                    break;
                }
            }
            j = 0;
        }
    }

    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winLow > measItem->adc_without_zero[i]) {
            measItem->fallP1 = i;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->fallP1 = EdgeFail;
        measItem->peridDataSum = EdgeFail;
        measItem->edgeBeing = 0;
        return;
    } else {
        measItem->edgeBeing = 1;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHigh < measItem->adc_without_zero[i]) {
            measItem->riseP1 = i;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->riseP1 = EdgeFail; //为负数代表无上升沿
        measItem->peridDataSum = EdgeFail;
        return;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winLow > measItem->adc_without_zero[i]) {
            measItem->fallP2 = i;
            measItem->peridDataSum = measItem->fallP2 - measItem->fallP1;
            return;
        }
    }

    measItem->fallP2 = EdgeFail; //为负数代表无下降沿
    measItem->peridDataSum = EdgeFail;
}

/**************************************************************************************************
 * 函数:  getVtopVbaseShoot
 * 说明:  获取顶端值和底端值,幅值，过冲，预冲
 * 输入:  
 *	 S32 edgeType	: 边沿选择	
 * 关联变量: 
 * 返回:  
 * 作者: 
 *--------------------------------------------------------------------------------------------------
 * 修改历史1 :   修改面积与周期面积的算法，去掉sum计算时所乘的数据个数。
 * 作者: 
 **************************************************************************************************/
void getVtopVbaseShoot(MeasureItem *measItem, S32 edgeType) {
    S32 i, temp = 0;
    S32 begin, end;
    S32 max_cnt = 0; // 最值统计: 用于出现多个相等最值的情况判断。
    static S32 data_valid[ADCSCALE]; // 0-无效，1-有效。
    static S32 ADData[ADCSCALE];
    S32 sum;
    S32 index;
    S32 statistics_num; // 总的统计数据个数
    F64 ADpercent; // 顶端值或低端值统计数据个数占总的统计数据个数百分比
    S32 offset2positive = ADCSCALE / 2 + measItem->zero; // 将ADC值转换为>=0的值。
    S32 step_value = measItem->stepValue;

    //计算面积
    sum = 0;
    for (i = 0; i < measItem->waveSize; i++) {
        sum += measItem->adc_without_zero[i];
    }
    measItem->area = (F64) sum * (F64) measItem->timePerData / 1000000.0;
    //当存在一个周期时，计算周期面积
    sum = 0;
    if (measItem->peridDataSum != EdgeFail) {
        if (RISE == edgeType) {
            for (i = measItem->riseP1; i < measItem->riseP2; i++) {
                sum += measItem->adc_without_zero[i];
            }
        } else {
            for (i = measItem->fallP1; i < measItem->fallP2; i++) {
                sum += measItem->adc_without_zero[i];
            }
        }
        measItem->cycleArea = ((F64) sum * (F64) measItem->timePerData / (F64) 1000000.0);
    } else {
        measItem->cycleArea = EdgeFail;
    }

    //初始化缓存
    for (i = 0; i < ADCSCALE; i++) {
        data_valid[i] = 0; // 初始化为无效标志
        ADData[i] = 0;
    }
    statistics_num = 0;

    if (RISE == edgeType) {
        if (EdgeFail == measItem->peridDataSum) {
            if (EdgeFail == measItem->riseP1) {
                measItem->Vtop = PvalueFail;
                measItem->Vbase = NvalueFail;
            } else {
                for (i = 0; i < measItem->waveSize; i = i + step_value) {
                    statistics_num++;
                    index = ADC_PRECISON_LIMIT & (measItem->adc_without_zero[i] + offset2positive);
                    data_valid[index] = 1;
                    ADData[index]++;
                }
            }
        } else//存在一个完整的周期
        {
            for (i = measItem->riseP1; i < measItem->riseP2; i = i + step_value) {
                statistics_num++;
                index = ADC_PRECISON_LIMIT & (measItem->adc_without_zero[i] + offset2positive);
                data_valid[index] = 1;
                ADData[index]++;
            }
        }
    } else {
        if (EdgeFail == measItem->peridDataSum) {
            if (EdgeFail == measItem->fallP1) {
                measItem->Vtop = PvalueFail;
                measItem->Vbase = NvalueFail;
            } else {
                for (i = 0; i < measItem->waveSize; i = i + step_value) {
                    statistics_num++;
                    index = ADC_PRECISON_LIMIT & (measItem->adc_without_zero[i] + offset2positive);
                    data_valid[index] = 1;
                    ADData[index]++;
                }
            }
        } else//存在一个完整的周期
        {
            for (i = measItem->fallP1; i < measItem->fallP2; i = i + step_value) {
                statistics_num++;
                index = ADC_PRECISON_LIMIT & (measItem->adc_without_zero[i] + offset2positive);
                data_valid[index] = 1;
                ADData[index]++;
            }
        }
    }

    if ((PvalueFail != measItem->Vtop) || (NvalueFail != measItem->Vbase)) {
        //将比较值清0，进行底端值比较
        temp = 0;
        max_cnt = 0;
        begin = measItem->min + offset2positive;
        begin &= ADC_PRECISON_LIMIT; //不能用min + offset2positive，会存在最小值大于被限制ADC精度时的情况
        end = ((measItem->max + measItem->min) / 2) + offset2positive;

        for (i = begin; i <= end; i++) {
            if ((data_valid[i] == 1) && (ADData[i] >= temp)) {
                if (ADData[i] == temp) {
                    max_cnt++;
                } else {
                    max_cnt = 1;
                    temp = ADData[i];
                }
                measItem->Vbase = i - offset2positive;

            }
        }
        ADpercent = (F64) temp / statistics_num;

        //        //检验系统执行时，不对顶端值赋极值，幅度更不会跳动更稳定
        //        if (!CheckSystemOn() && (max_cnt >= 2
        //                || (temp == 2 && (edgeValue->peridDataSum > X_DIV_POINTS))))// 屏蔽三角波计算时出错的问题
        //        {
        //            arith_ch->Vbase = arith_ch->min;
        //        }

        //将比较值清0，进行顶端值比较
        temp = 0;
        max_cnt = 0;
        begin = ((measItem->max + measItem->min) / 2) + offset2positive;
        end = measItem->max + offset2positive;

        for (i = begin; i <= end; i++) {
            if ((data_valid[i] == 1) && (ADData[i] >= temp)) {
                if (ADData[i] == temp) {
                    max_cnt++;
                } else {
                    max_cnt = 1;
                    temp = ADData[i];
                }
                measItem->Vtop = i - offset2positive;

            }
        }
        ADpercent = (F64) temp / statistics_num;

        //        //检验系统执行时，不对顶端值赋极值，幅度更不会跳动更稳定
        //        if (!CheckSystemOn() && (max_cnt >= 2
        //                || (temp == 2 && (edgeValue->peridDataSum > X_DIV_POINTS))))// 屏蔽三角波计算时出错的问题
        //        {
        //            arith_ch->Vtop = arith_ch->max;
        //        }

        measItem->Vamp = measItem->Vtop - measItem->Vbase;
        if (0 == measItem->Vamp) {
            measItem->VOverShoot = EdgeFail;
            measItem->VPreShoot = EdgeFail;
        } else {
            measItem->Vhigh = (S32) ((F64) measItem->Vamp * 0.9) + measItem->Vbase;
            measItem->Vmid = (S32) ((F64) measItem->Vamp * 0.5) + measItem->Vbase;
            measItem->Vlow = (S32) ((F64) measItem->Vamp * 0.1) + measItem->Vbase;
            measItem->VOverShoot = (F32) (abs(measItem->max - measItem->Vtop)) / (F32) measItem->Vamp;
            measItem->VPreShoot = (F32) (abs(measItem->min - measItem->Vbase)) / (F32) measItem->Vamp;
        }
    }
}

/******************************************************************************
 * 函数:  getCycleRMS
 *        
 * 说明:  获取真均方根值
 *        
 * 输入:  		
 *        
 * 返回:  
 *        
 * 作者: 
 *--------------------------------------------------------------------------------------------------

 ******************************************************************************/
void getCycleRMS(MeasureItem *measItem, S32 edgeType) {
    S32 i;
    F32 cycleRMS;
    F64 square = 0;

    if (RISE == edgeType) {
        //存在一个完整的周期
        if (EdgeFail != measItem->peridDataSum) {
            for (i = measItem->riseP1; i < measItem->riseP2; i++) {
                square += (F64) measItem->adc_without_zero[i] * (F64) measItem->adc_without_zero[i];
            }
            if (0 != (measItem->riseP2 - measItem->riseP1)) {
                square = square / ((F64) (measItem->riseP2 - measItem->riseP1));
            } else {
                square = 0;
            }
            cycleRMS = (F32) sqrt(square);
        } else {
            cycleRMS = EdgeFail;
        }
    } else {
        //存在一个完整的周期
        if (EdgeFail != measItem->peridDataSum) {
            for (i = measItem->fallP1; i < measItem->fallP2; i++) {
                square += (F64) measItem->adc_without_zero[i] * (F64) measItem->adc_without_zero[i];
            }
            if (0 != (measItem->fallP2 - measItem->fallP1)) {
                square = square / ((F64) (measItem->fallP2 - measItem->fallP1));
            } else {
                square = 0;
            }
            cycleRMS = (F32) sqrt(square);
        } else {
            cycleRMS = EdgeFail;
        }
    }

    measItem->cycleRMS = cycleRMS;
}

/******************************************************************
 * 函数名:measureItemRiseFallTime 
 * 功能描述: - 
 *    获取上升沿和下降沿的时间 
 * 输入: 
 * 输出: 
 * 返回值: 
 * 
 * 作者: 
 * 日期:
 * 备注:
 * -----------------------------------------
 * 修改历史
 * 1、修改人:
 * 修改内容:
 * 修改日期:
 * -----------------------------------------
 *****************************************************************/
void measureItemRiseFallTime(MeasureItem *measItem) {
    S32 i;
    S32 winLow, winHigh;
    S32 firstEdgePos = -1;
    S32 firstEdgeType; // 0--上升沿，1--下降沿。

    if (measItem->Vtop == PvalueFail || measItem->Vbase == NvalueFail) {
        winHigh = (measItem->max + measItem->min) / 2 + AMP_DATA_WINDOW;
        winLow = (measItem->max + measItem->min) / 2 - AMP_DATA_WINDOW;
    } else {
        winHigh = (S32) ((F64) measItem->Vamp * 0.9) + measItem->Vbase;
        winLow = (S32) ((F64) measItem->Vamp * 0.1) + measItem->Vbase;
    }


    firstEdgePos = findFristEdgePosition(measItem, winHigh, winLow, &firstEdgeType);

    if (firstEdgePos < 0) {
        measItem->riseTime = EdgeFail;
        measItem->fallTime = EdgeFail;
    } else {
        if (RISE == firstEdgeType) {
            getFirstRiseEdge(measItem, firstEdgePos, winHigh, winLow);
        } else {
            getFirstFallEdge(measItem, firstEdgePos, winHigh, winLow);
        }

        getEdgeNum(measItem, firstEdgePos, winHigh, winLow);
    }
}

/******************************************************************
 * 函数名:_get_firstRiseEdge 
 * 功能描述: - 
 *    第一个沿是上升沿时计算上升时间 
 * 输入: 
 * 输出: 
 * 返回值: 
 * 
 * 作者:
 * 日期:
 * 备注:
 * -----------------------------------------
 * 修改历史
 * 1、修改人:
 * 修改内容:
 * 修改日期:
 * -----------------------------------------
 *****************************************************************/
void getFirstRiseEdge(MeasureItem *measItem, S32 start, S32 winHigh, S32 winLow) {
    S32 i;
    S32 riseP1, riseP2, fallP1, fallP2;

    riseP1 = start;
    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winLow > measItem->adc_without_zero[i]) {
            riseP1 = i;
        }

        if (winHigh <= measItem->adc_without_zero[i]) {
            riseP2 = i;
            measItem->riseTime = riseP2 - riseP1;
            measItem->risePos = riseP1;
            measItem->firstEdge = 0;
            break;
        }
    }
    if (i >= measItem->waveSize) {
        measItem->riseTime = EdgeFail; //为负数代表无上升沿，无法计算顶端值和底端值
        measItem->fallTime = EdgeFail;
        measItem->firstEdge = EdgeFail;
        return;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHigh <= measItem->adc_without_zero[i]) {
            fallP1 = i;
        }
        if (winLow > measItem->adc_without_zero[i]) {
            fallP2 = i;
            measItem->fallTime = fallP2 - fallP1;
            measItem->fallPos = fallP1;
            return;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->fallTime = EdgeFail; //为负数代表无下降沿	
        return;
    }
}

/******************************************************************
 * 函数名:getFirstFallEdge 
 * 功能描述: - 
 *    第一个沿是下降沿时计算下降时间 
 * 输入: 
 * 输出: 
 * 返回值: 
 * 
 * 作者:
 * 日期:
 * 备注:
 * -----------------------------------------
 * 修改历史
 * 1、修改人:
 * 修改内容:
 * 修改日期:
 * -----------------------------------------
 *****************************************************************/
void getFirstFallEdge(MeasureItem *measItem, S32 start, S32 winHigh, S32 winLow) {
    S32 i;
    S32 fallP1, fallP2, riseP1, riseP2;

    fallP1 = start;
    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {

        if (winHigh < measItem->adc_without_zero[i]) {
            fallP1 = i;
        }

        if (winLow >= measItem->adc_without_zero[i]) {
            fallP2 = i;
            measItem->fallTime = fallP2 - fallP1;
            measItem->fallPos = fallP1;
            measItem->firstEdge = 1;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->riseTime = EdgeFail; //为负数代表无上升沿，无法计算顶端值和底端值
        measItem->fallTime = EdgeFail;
        measItem->firstEdge = EdgeFail;
        return;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winLow > measItem->adc_without_zero[i]) {
            riseP1 = i;

        }
        if (winHigh <= measItem->adc_without_zero[i]) {
            riseP2 = i;
            measItem->riseTime = riseP2 - riseP1;
            measItem->risePos = riseP1;
            return;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->riseTime = EdgeFail;
        return;
    }
}

/**************************************************************************************************
 * 函数: getEdgeNum
 * 说明:  获取上升沿，下降沿个数,有上升沿的判断标准是从10%－90%之间有数据即表示有一段上升沿
 * 输入:  
 * 关联变量: 
 * 返回:  
 * 作者:
 **************************************************************************************************/
void getEdgeNum(MeasureItem *measItem, S32 start, S32 winHigh, S32 winLow) {
    S32 i;
    S32 rise_edge_num = 0;
    S32 fall_edga_num = 0;
    S32 rise_P1_flag = 0;
    S32 fall_P1_flag = 0;

    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {
        //查询上升沿第一个参考点
        if (winLow > measItem->adc_without_zero[i]) {
            rise_P1_flag = 1;
        }
        //找到第二个参考点则计数一个上升沿
        if (winHigh <= measItem->adc_without_zero[i] && rise_P1_flag == 1) {
            rise_P1_flag = 0;
            rise_edge_num++;
        }
    }

    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {
        //查询第一个下降点
        if (winHigh <= measItem->adc_without_zero[i]) {
            fall_P1_flag = 1;
        }
        //找到第二个参考点，则计一个下降沿
        if (winLow > measItem->adc_without_zero[i] && fall_P1_flag == 1) {
            fall_P1_flag = 0;
            fall_edga_num++;
        }
    }

    measItem->fallingEdgeNum = fall_edga_num;
    measItem->risingEdgeNum = rise_edge_num;
}

void measureItemPulseWidth(MeasureItem *measItem) {
    S32 i, j;
    S32 winHalf, winLow, winHigh;
    S32 firstEdgePos = -1;
    S32 firstEdgeType; // 0--上升沿，1--下降沿。

    if (measItem->Vtop == PvalueFail || measItem->Vbase == NvalueFail) {
        winHalf = (measItem->max + measItem->min) / 2;
    } else {
        winHalf = measItem->Vamp / 2 + measItem->Vbase;
    }

    winHigh = winHalf + AMP_DATA_WINDOW_OF_PULSE_WIDTH;
    winLow = winHalf - AMP_DATA_WINDOW_OF_PULSE_WIDTH;

    measItem->PWidth = EdgeFail;
    measItem->NWidth = EdgeFail;

    firstEdgePos = findFristEdgePosition(measItem, winHigh, winLow, &firstEdgeType);

    if (firstEdgePos >= 0) {
        if (firstEdgeType == FALL) {
            getFirstNWidth(measItem, firstEdgePos, winHalf, winHigh, winLow);
        } else {
            getFirstPWidth(measItem, firstEdgePos, winHalf, winHigh, winLow);
        }
    }

    if ((EdgeFail != measItem->PWidth) && (EdgeFail != measItem->NWidth)) {
        if (0 != (measItem->PWidth + measItem->NWidth)) {
            measItem->PDuty = (F32) measItem->PWidth / ((F32) measItem->PWidth + (F32) measItem->NWidth);
            measItem->NDuty = (F32) measItem->NWidth / ((F32) measItem->PWidth + (F32) measItem->NWidth);
        } else {
            measItem->PDuty = EdgeFail;
            measItem->NDuty = EdgeFail;
            measItem->cycleRMS = EdgeFail;
        }
    } else {
        measItem->PDuty = EdgeFail;
        measItem->NDuty = EdgeFail;
        measItem->cycleRMS = EdgeFail;
    }
}

void getFirstNWidth(MeasureItem *measItem, S32 start, S32 winHalf, S32 winHigh, S32 winLow) {
    S32 i;
    S32 riseP1, riseP2, fallP1, fallP2;

    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHalf >= measItem->adc_without_zero[i]) {
            fallP1 = i;
            measItem->fallFirst = fallP1;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无正脉宽
        measItem->NWidth = EdgeFail;
        return;
    }

    //判断是否超过窗口低电平，作滤波
    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winLow >= measItem->adc_without_zero[i]) {
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无负脉宽
        measItem->NWidth = EdgeFail; //为负数代表无负脉宽
        return;
    }


    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHalf <= measItem->adc_without_zero[i]) {
            riseP1 = i;
            measItem->riseFirst = riseP1;
            measItem->NWidth = riseP1 - fallP1;
            break;
        }

    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无正脉宽
        measItem->NWidth = EdgeFail;
        return;

    }

    //判断是否超过窗口高电平，作滤波
    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHigh <= measItem->adc_without_zero[i]) {
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无正脉宽
        return;

    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHalf >= measItem->adc_without_zero[i]) {
            fallP2 = i;
            measItem->fallSecond = fallP2;
            measItem->PWidth = fallP2 - riseP1;
            return;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无负脉宽		
        return;
    }
}

void getFirstPWidth(MeasureItem *measItem, S32 start, S32 winHalf, S32 winHigh, S32 winLow) {
    S32 i;
    S32 riseP1, riseP2, fallP1, fallP2;

    for (i = start; i < measItem->waveSize; i = i + measItem->stepValue) {

        if (winHalf <= measItem->adc_without_zero[i]) {
            riseP1 = i;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无正脉宽
        measItem->NWidth = EdgeFail;
        return;
    }

    //判断是否超过窗口高电平，作滤波
    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHigh <= measItem->adc_without_zero[i]) {
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无负脉宽
        measItem->NWidth = EdgeFail; //为负数代表无负脉宽
        return;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHalf >= measItem->adc_without_zero[i]) {
            fallP1 = i;
            measItem->fallFirst = fallP1;
            measItem->PWidth = fallP1 - riseP1;
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->PWidth = EdgeFail; //为负数代表无正脉宽
        measItem->NWidth = EdgeFail;
        return;
    }

    //判断是否超过窗口低电平，作滤波
    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winLow >= measItem->adc_without_zero[i]) {
            break;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->NWidth = EdgeFail; //为负数代表无负脉宽
        return;
    }

    for (; i < measItem->waveSize; i = i + measItem->stepValue) {
        if (winHalf <= measItem->adc_without_zero[i]) {
            riseP2 = i;
            measItem->riseSecond = riseP2;
            measItem->NWidth = riseP2 - fallP1;
            return;
        }
    }

    if (i >= measItem->waveSize) {
        measItem->NWidth = EdgeFail;
        return;
    }
}

/**************************************************************************************************
 * 函数:  void measureItemFreqCycle(MeasureItem *measItem)
 * 说明:  
 * 输入:  
	
 * 关联变量: 
 * 返回:  
 * 作者:  
 *--------------------------------------------------------------------------------------------------
 * 修改历史1 :   修改周期个数和数据个数搜索方法。
 * 作者:
 **************************************************************************************************/
void measureItemFreqCycle(MeasureItem *measItem) {
    S32 i;
    S32 rise_pos = -1, rise_pos_last = -1;
    S32 fall_pos = -1, fall_pos_last = -1;
    S32 n_rise = 0; // 上升沿序号
    S32 n_fall = 0; // 下降沿序号
    S32 value_above_high = 0; // 值>=高值标志，0--<，1-->=。
    S32 value_below_low = 0; // 值<=低值标志，0-->，1--<=。
    S32 win_low, win_high; //中间值窗口，上下限
    S32 win_data_counter = 0, win_cyc_counter = 0; //屏幕数据个数，周期个数
    S32 win_data_counter_rise = 0, win_data_counter_fall = 0; //上升沿屏幕数据个数，下降沿屏幕数据个数
    S32 cycle_data_counter_rise = 0, cycle_data_counter_fall = 0; //上升沿周期数据个数，下降沿周期数据个数
    S32 firstEdgePos = -1;
    S32 firstEdgeType; // 0--上升沿，1--下降沿。

    S32 search_start = 0;

    if (fabs(measItem->max - measItem->average) >= fabs(measItem->min - measItem->average)) {
        search_start = measItem->maxdata_position % 2;
    } else {
        search_start = measItem->mindata_position % 2;
    }

    if (measItem->Vtop == PvalueFail || measItem->Vbase == NvalueFail) {
        win_high = (measItem->max + measItem->min) / 2 + AMP_DATA_WINDOW;
        win_low = (measItem->max + measItem->min) / 2 - AMP_DATA_WINDOW;
    } else {
        win_low = (S32) ((F64) measItem->Vamp * 0.1) + measItem->Vbase;
        win_high = (S32) ((F64) measItem->Vamp * 0.9) + measItem->Vbase;
    }

    firstEdgePos = findFristEdgePosition(measItem, win_high, win_low, &firstEdgeType);

    if (firstEdgePos < 0) {
        n_rise = 0;
        n_fall = 0;
    } else {
        for (i = search_start; i < measItem->waveSize; i = i + measItem->stepValue) {
            // 查找上升沿位置
            if (measItem->adc_without_zero[i] <= win_low) {
                rise_pos = i;
            }
            if (rise_pos != -1 && value_above_high == 0 && measItem->adc_without_zero[i] >= win_high) {
                value_above_high = 1;
                n_rise++;
                if (n_rise == 1) {
                    measItem->riseFirst = rise_pos;
                }
                if (n_rise == 2) {
                    measItem->riseSecond = rise_pos;
                }
                if (n_rise > 1) {
                    cycle_data_counter_rise = rise_pos - rise_pos_last;
                    win_data_counter_rise += cycle_data_counter_rise;
                }
                rise_pos_last = rise_pos;
            }
            if (value_above_high == 1 && measItem->adc_without_zero[i] < win_high) {
                if (i - rise_pos_last >= measItem->stepValue) {
                    value_above_high = 0;
                    rise_pos = -1;
                    if (n_rise >= 250) {
                        break;
                    }
                }
            }

            // 查找下降沿位置
            if (measItem->adc_without_zero[i] >= win_high) {
                fall_pos = i;
            }
            if (fall_pos != -1 && value_below_low == 0 && measItem->adc_without_zero[i] <= win_low) {
                value_below_low = 1;
                n_fall++;
                if (n_fall == 1) {
                    measItem->fallFirst = fall_pos;
                }
                if (n_fall == 2) {
                    measItem->fallSecond = fall_pos;
                }
                if (n_fall > 1) {
                    cycle_data_counter_fall = fall_pos - fall_pos_last;
                    win_data_counter_fall += cycle_data_counter_fall;
                }
                fall_pos_last = fall_pos;
            }
            if (value_below_low == 1 && measItem->adc_without_zero[i] > win_low) {
                if (i - fall_pos_last >= measItem->stepValue) {
                    value_below_low = 0;
                    fall_pos = -1;
                    if (n_fall >= 250) {
                        break;
                    }
                }
            }
        }
    }

    // FPGA压缩数据，先小后大，当密集时，容易产生周期误判。
    if ((n_rise < 2 && n_fall < 2)
            || (n_rise > 100 && n_fall < 100) // 波形密集时，由于上面描述的压缩原因，不进行计算。
            || (n_rise < 100 && n_fall > 100)
            || (n_rise > 200 && n_fall > 200)) {
        win_cyc_counter = 0;
        win_data_counter = 0;
    } else if (n_rise >= n_fall) {
        win_cyc_counter = n_rise - 1;
        win_data_counter = win_data_counter_rise;
    } else {
        win_cyc_counter = n_fall - 1;
        win_data_counter = win_data_counter_fall;
    }

    measItem->cpu_calc_cyc_counter = win_cyc_counter;
    measItem->cpu_calc_data_counter = win_data_counter;
    measItem->cpu_calc_fullscreen = measItem->waveSize;
}

//***********************************************************

void measureDelayCalc(MeasureParam *measParam) {
    MeasureDelay *measDelay = &measParam->measDelay;
    S32 cha, chb;
    S32 i;

    for (cha = 0; cha < CH_TOTAL; cha++) {
        for (chb = 0; chb < CH_TOTAL; chb++) {
            for (i = RISEDELAY; i <= MEASURE_DEALY_TOTAL; i++) {
                measDelay->valid[i][cha][chb] = FALSE;
            }

            if (measParam->measItem[cha].onoff == ON && measParam->measItem[chb].onoff == ON) {
                getDelayTime(measParam, cha, chb);
            }
        }
    }
}

void getDelayTime(MeasureParam *measParam, S32 cha, S32 chb) {
    MeasureItem *measItemA = &measParam->measItem[cha];
    MeasureItem *measItemB = &measParam->measItem[chb];
    MeasureDelay *measDelay = &measParam->measDelay;

    getDelayTimeEdgePostion(measItemA);
    getDelayTimeEdgePostion(measItemB);

    // FRR	
    if ((EdgeFail != measItemA->delay_first_rise_edge_pos) && (EdgeFail != measItemB->delay_first_rise_edge_pos)) {
        measDelay->FRR[cha][chb] = -(measItemA->delay_first_rise_edge_pos - measItemB->delay_first_rise_edge_pos);
        measDelay->valid[DELAY_FRR][cha][chb] = TRUE;
    }
    // FRF	
    if ((EdgeFail != measItemA->delay_first_rise_edge_pos) && (EdgeFail != measItemB->delay_first_fall_edge_pos)) {
        measDelay->FRF[cha][chb] = -(measItemA->delay_first_rise_edge_pos - measItemB->delay_first_fall_edge_pos);
        measDelay->valid[DELAY_FRF][cha][chb] = TRUE;
    }

    // FFR	
    if ((EdgeFail != measItemA->delay_first_fall_edge_pos) && (EdgeFail != measItemB->delay_first_rise_edge_pos)) {
        measDelay->FFR[cha][chb] = -(measItemA->delay_first_fall_edge_pos - measItemB->delay_first_rise_edge_pos);
        measDelay->valid[DELAY_FFR][cha][chb] = TRUE;
    }

    // FFF
    if ((EdgeFail != measItemA->delay_first_fall_edge_pos) && (EdgeFail != measItemB->delay_first_fall_edge_pos)) {
        measDelay->FFF[cha][chb] = -(measItemA->delay_first_fall_edge_pos - measItemB->delay_first_fall_edge_pos);
        measDelay->valid[DELAY_FFF][cha][chb] = TRUE;
    }

    // LRR	
    if ((EdgeFail != measItemA->delay_first_rise_edge_pos) && (EdgeFail != measItemB->delay_last_rise_edge_pos)) {
        measDelay->LRR[cha][chb] = -(measItemA->delay_first_rise_edge_pos - measItemB->delay_last_rise_edge_pos);
        measDelay->valid[DELAY_LRR][cha][chb] = TRUE;
    }

    // LRF	
    if ((EdgeFail != measItemA->delay_first_rise_edge_pos) && (EdgeFail != measItemB->delay_last_fall_edge_pos)) {
        measDelay->LRF[cha][chb] = -(measItemA->delay_first_rise_edge_pos - measItemB->delay_last_fall_edge_pos);
        measDelay->valid[DELAY_LRF][cha][chb] = TRUE;
    }

    // LFR	
    if ((EdgeFail != measItemA->delay_first_fall_edge_pos) && (EdgeFail != measItemB->delay_last_rise_edge_pos)) {
        measDelay->LFR[cha][chb] = -(measItemA->delay_first_fall_edge_pos - measItemB->delay_last_rise_edge_pos);
        measDelay->valid[DELAY_LFR][cha][chb] = TRUE;
    }

    // LFF
    if ((EdgeFail != measItemA->delay_first_fall_edge_pos) && (EdgeFail != measItemB->delay_last_fall_edge_pos)) {
        measDelay->LFF[cha][chb] = -(measItemA->delay_first_fall_edge_pos - measItemB->delay_last_fall_edge_pos);
        measDelay->valid[DELAY_LFF][cha][chb] = TRUE;
    }


    getDelayRRFF_time(measParam, cha, chb);
}

/**************************************************************************************************
 * 函数:  getDelayTimeEdgePostion
 * 说明:  获取延迟边沿位置：第一个上升沿、下降沿，最后一个上升沿、下降沿。
 * 输入:  
 *			
 * 关联变量: 
 * 返回:  
 * 作者: 
 **************************************************************************************************/
void getDelayTimeEdgePostion(MeasureItem *measItem) {
    // 上升沿位置=下降沿位置=低端值+幅值*50%；
    S32 i;
    S32 riseP1 = -1, fallP1 = -1;
    S32 value_above_high = 0; // 值>=高值标志，0--<，1-->=。
    S32 value_below_low = 0; // 值<=低值标志，0-->，1--<=。
    S32 delay_first_rise_edge_pos_flag = 0; // 0--未找到，1--找到。
    S32 delay_first_fall_edge_pos_flag = 0; // 0--未找到，1--找到。

    measItem->delay_first_rise_edge_pos = EdgeFail;
    measItem->delay_first_fall_edge_pos = EdgeFail;
    measItem->delay_last_rise_edge_pos = EdgeFail;
    measItem->delay_last_fall_edge_pos = EdgeFail;

    for (i = 0; i < measItem->waveSize; i = i + measItem->stepValue) {
        // 查找上升沿位置
        if (measItem->adc_without_zero[i] <= measItem->Vmid) {
            riseP1 = i;
        }
        if (riseP1 != -1 && measItem->adc_without_zero[i] >= measItem->Vhigh) {
            value_above_high = 1;
            if (delay_first_rise_edge_pos_flag == 0) {
                delay_first_rise_edge_pos_flag = 1;
                measItem->delay_first_rise_edge_pos = riseP1;
            } else {
                measItem->delay_last_rise_edge_pos = riseP1;
            }
        }
        if (value_above_high == 1 && measItem->adc_without_zero[i] < measItem->Vhigh) {
            value_above_high = 0;
            riseP1 = -1;
        }

        // 查找下降沿位置
        if (measItem->adc_without_zero[i] >= measItem->Vmid) {
            fallP1 = i;
        }
        if (fallP1 != -1 && measItem->adc_without_zero[i] <= measItem->Vlow) {
            value_below_low = 1;
            if (delay_first_fall_edge_pos_flag == 0) {
                delay_first_fall_edge_pos_flag = 1;
                measItem->delay_first_fall_edge_pos = fallP1;
            } else {
                measItem->delay_last_fall_edge_pos = fallP1;
            }
        }
        if (value_below_low == 1 && measItem->adc_without_zero[i] < measItem->Vlow) {
            value_below_low = 0;
            fallP1 = -1;
        }
    }

    // 当波形仅有一个上升/下降沿时， 第一个沿与最后一个沿为同一个边沿.
    if (measItem->delay_first_rise_edge_pos != EdgeFail && measItem->delay_last_rise_edge_pos == EdgeFail) {
        measItem->delay_last_rise_edge_pos = measItem->delay_first_rise_edge_pos;
    }
    if (measItem->delay_first_fall_edge_pos != EdgeFail && measItem->delay_last_fall_edge_pos == EdgeFail) {
        measItem->delay_last_fall_edge_pos = measItem->delay_first_fall_edge_pos;
    }

    if (delay_first_rise_edge_pos_flag == 0) {
        measItem->delay_first_rise_edge_pos = EdgeFail;
        measItem->delay_last_rise_edge_pos = EdgeFail;
    }

    if (delay_first_fall_edge_pos_flag == 0) {
        measItem->delay_first_fall_edge_pos = EdgeFail;
        measItem->delay_last_fall_edge_pos = EdgeFail;
    }
}

/**************************************************************************************************
 * 函数:   getDelayRRFF_time
 * 说明:  获取上升沿/下降沿延迟
 * 输入:  
 *	
 * 关联变量: 
 * 返回:  
 * 作者:  
 **************************************************************************************************/
void getDelayRRFF_time(MeasureParam *measParam, S32 cha, S32 chb) {
    S32 i;
    S32 riseP1 = -1, fallP1 = -1;
    S32 delay_rise_edge_pos = EdgeFail; // edgeFail--未找到，>=0--找到。
    S32 delay_fall_edge_pos = EdgeFail; // edgeFail--未找到，>=0--找到。

    MeasureItem *measItemA = &measParam->measItem[cha];
    MeasureItem *measItemB = &measParam->measItem[chb];
    MeasureDelay *measDelay = &measParam->measDelay;

    if (measItemA->delay_first_rise_edge_pos != EdgeFail) {
        for (i = measItemA->delay_first_rise_edge_pos; i < measItemB->waveSize; i = i + measItemB->stepValue) {
            // 查找上升沿位置
            if (measItemB->adc_without_zero[i] <= measItemB->Vmid) {
                riseP1 = i;
            }
            if (riseP1 != -1 && measItemB->adc_without_zero[i] >= measItemB->Vhigh) {
                delay_rise_edge_pos = riseP1;
                break;
            }
        }
    }
    if (measItemA->delay_first_fall_edge_pos != EdgeFail) {
        for (i = measItemA->delay_first_fall_edge_pos; i < measItemB->waveSize; i = i + measItemB->stepValue) {
            // 查找下降沿位置
            if (measItemB->adc_without_zero[i] >= measItemB->Vmid) {
                fallP1 = i;
            }
            if (fallP1 != -1 && measItemB->adc_without_zero[i] <= measItemB->Vlow) {
                delay_fall_edge_pos = fallP1;
                break;
            }
        }
    }

    if (delay_rise_edge_pos != EdgeFail) {
        measDelay->delayRR[cha][chb] = (delay_rise_edge_pos - measItemA->delay_first_rise_edge_pos);
        measDelay->valid[RISEDELAY][cha][chb] = TRUE;
    }

    if (delay_fall_edge_pos != EdgeFail) {
        measDelay->delayFF[cha][chb] = (delay_fall_edge_pos - measItemA->delay_first_fall_edge_pos);
        measDelay->valid[FALLDELAY][cha][chb] = TRUE;
    }
}


//*************************************************************************

void measureResult(MeasureParam *measParam, MeasureResult *result) {
    S32 ch, cha, chb;
    F64 voltUnit;
    F64 timePerData;
    MeasureItem *measItem;
    MeasureDelay *measDelay;
    MeasureItemResult *measItemR;
    MeasureItemResult *measItemA, *measItemB;
    MeasureDelayResult *measDelayR;

    for (ch = CH1; ch < CH_TOTAL; ch++) {
        measItem = &measParam->measItem[ch];
        measItemR = &result->measItem[ch];
        timePerData = measItem->timePerData;
        voltUnit = getVoltageValuesPerData(measItem->voltScale);
        measItem->voltUnit = voltUnit;

        measItemR->overflow = measItem->overflow;
        measItemR->max = voltUnit * measItem->max;
        measItemR->min = voltUnit * measItem->min;
        measItemR->pk_pk = voltUnit * measItem->pk_pk;
        measItemR->Vtop = voltUnit * measItem->Vtop;
        measItemR->Vbase = voltUnit * measItem->Vbase;
        measItemR->Vamp = voltUnit * measItem->Vamp;
        measItemR->VoverShoot = measItem->VOverShoot;
        measItemR->VpreShoot = measItem->VPreShoot;
        measItemR->average = voltUnit * measItem->average;
        measItemR->Vrms = voltUnit * measItem->Vrms;
        measItemR->pauseRMS = voltUnit * measItem->pauseRMS;
        if (measItem->cycleRMS < -1.000001 || measItem->cycleRMS > -0.999999) {
            measItemR->cycleRMS = voltUnit * measItem->cycleRMS;
        } else {
            measItemR->cycleRMS = OVERFLOWVALUE;
        }

        measItemR->PWidth = timePerData * measItem->PWidth;
        measItemR->NWidth = timePerData * measItem->NWidth;
        measItemR->PDuty = measItem->PDuty;
        measItemR->NDuty = measItem->NDuty;

        if (measItem->riseTime < 0) {
            measItemR->riseTime = OVERFLOWTIME;
        } else {
            measItemR->riseTime = timePerData * measItem->riseTime;
        }

        if (measItem->fallTime < 0) {
            measItemR->fallTime = OVERFLOWTIME;
        } else {
            measItemR->fallTime = timePerData * measItem->fallTime;
        }

        if (measItem->cpu_calc_data_counter == 0 || measItem->cpu_calc_cyc_counter == 0) {
            measItemR->period = OVERFLOWTIME;
            measItemR->freq = OVERFLOWTIME;
            measItemR->PDuty = OVERFLOWTIME;
            measItemR->NDuty = OVERFLOWTIME;

        } else {
            measItemR->period = timePerData * (F64) measItem->cpu_calc_data_counter / (F64) measItem->cpu_calc_cyc_counter;
            measItemR->freq = (F64) 1000000 / measItemR->period;
        }



        measItemR->PPulseNum = measItem->PPulseNum;
        measItemR->NPulseNum = measItem->NPulseNum;
        measItemR->risingEdgeNum = measItem->risingEdgeNum;
        measItemR->fallingEdgeNum = measItem->fallingEdgeNum;

        measItemR->area = voltUnit * measItem->area;
        if (measItem->risingEdgeNum != 0 && measItem->fallingEdgeNum != 0) {
            measItemR->cycleArea = voltUnit * measItem->cycleArea;
        } else {
            measItemR->cycleArea = OVERFLOWTIME;
        }

        if (measItem->riseTime < 0 || measItem->fallTime < 0) {
            measItemR->PPulseNum = 0;
            measItemR->NPulseNum = 0;
            measItemR->cycleArea = OVERFLOWTIME;
        }
        if (measItem->riseTime < 0) {
            measItemR->risingEdgeNum = 0;
        }
        if (measItem->fallTime < 0) {
            measItemR->fallingEdgeNum = 0;
        }

        if ((PvalueFail == measItem->Vtop) || (NvalueFail == measItem->Vbase)) {
            measItemR->Vtop = OVERFLOWVALUE;
            measItemR->Vbase = OVERFLOWVALUE;
            measItemR->Vamp = OVERFLOWVALUE;
            measItemR->VoverShoot = OVERFLOWVALUE;
            measItemR->VpreShoot = OVERFLOWVALUE;
            measItemR->cycleRMS = OVERFLOWVALUE;

            measItemR->PDuty = OVERFLOWTIME;
            measItemR->NDuty = OVERFLOWTIME;

            measItemR->PWidth = OVERFLOWTIME;
            measItemR->NWidth = OVERFLOWTIME;

            measItemR->riseTime = OVERFLOWTIME;
            measItemR->fallTime = OVERFLOWTIME;
        }
    }

    measDelay = &measParam->measDelay;
    measDelayR = &result->measDelay;

    for (cha = CH1; cha < CH_TOTAL; cha++) {
        for (chb = CH1; chb < CH_TOTAL; chb++) {
            measItemA = &result->measItem[cha];
            measItemB = &result->measItem[chb];

            measDelayR->FRR[cha][chb] = timePerData * measDelay->FRR[cha][chb];
            if (measDelay->valid[DELAY_FRR][cha][chb] == FALSE) {
                measDelayR->FRR[cha][chb] = OVERFLOWVALUE;
            }

            measDelayR->FRF[cha][chb] = timePerData * measDelay->FRF[cha][chb];
            if (measDelay->valid[DELAY_FRF][cha][chb] == FALSE) {
                measDelayR->FRF[cha][chb] = OVERFLOWVALUE;
            }

            measDelayR->FFR[cha][chb] = timePerData * measDelay->FFR[cha][chb];
            if (measDelay->valid[DELAY_FFR][cha][chb] == FALSE) {
                measDelayR->FFR[cha][chb] = OVERFLOWVALUE;
            }

            measDelayR->FFF[cha][chb] = timePerData * measDelay->FFF[cha][chb];
            if (measDelay->valid[DELAY_FFF][cha][chb] == FALSE) {
                measDelayR->FFF[cha][chb] = OVERFLOWVALUE;
            }

            measDelayR->LRR[cha][chb] = timePerData * measDelay->LRR[cha][chb];
            if (measDelay->valid[DELAY_LRR][cha][chb] == FALSE) {
                measDelayR->LRR[cha][chb] = OVERFLOWVALUE;
            }

            measDelayR->LRF[cha][chb] = timePerData * measDelay->LRF[cha][chb];
            if (measDelay->valid[DELAY_LRF][cha][chb] == FALSE) {
                measDelayR->LRF[cha][chb] = OVERFLOWVALUE;
            }

            measDelayR->LFR[cha][chb] = timePerData * measDelay->LFR[cha][chb];
            if (measDelay->valid[DELAY_LFR][cha][chb] == FALSE) {
                measDelayR->LFR[cha][chb] = OVERFLOWVALUE;
            }

            measDelayR->LFF[cha][chb] = timePerData * measDelay->LFF[cha][chb];
            if (measDelay->valid[DELAY_LFF][cha][chb] == FALSE) {
                measDelayR->LFF[cha][chb] = OVERFLOWVALUE;
            }


            measDelayR->delayRR[cha][chb] = timePerData * measDelay->delayRR[cha][chb];
            measDelayR->delayFF[cha][chb] = timePerData * measDelay->delayFF[cha][chb];

            if (EdgeFail != measItemA->PDuty) {
                if ((measItemA->period > 0.000001 || measItemA->period < -0.000001) && measItemA->period != OVERFLOWTIME) {
                    measDelayR->phaseRR[cha][chb] = measDelayR->delayRR[cha][chb] / measItemA->period * 360.0;
                } else {
                    measDelayR->phaseRR[cha][chb] = OVERFLOWVALUE;
                }
            } else {
                measDelayR->phaseRR[cha][chb] = OVERFLOWVALUE;
            }

            if (measDelay->valid[RISEDELAY][cha][chb] == FALSE) {
                measDelayR->delayRR[cha][chb] = OVERFLOWVALUE;
                measDelayR->phaseRR[cha][chb] = OVERFLOWVALUE;
            }


            if (EdgeFail != measItemA->NDuty) {
                if ((measItemA->period > 0.000001 || measItemA->period < -0.000001) && measItemA->period != OVERFLOWTIME) {
                    measDelayR->phaseFF[cha][chb] = measDelayR->delayFF[cha][chb] / measItemA->period * 360.0;
                } else {
                    measDelayR->phaseFF[cha][chb] = OVERFLOWVALUE;
                }
            } else {
                measDelayR->phaseFF[cha][chb] = OVERFLOWVALUE;
            }

            if (measDelay->valid[FALLDELAY][cha][chb] == FALSE) {
                measDelayR->delayFF[cha][chb] = OVERFLOWVALUE;
                measDelayR->phaseFF[cha][chb] = OVERFLOWVALUE;
            }

        }
    }
}

/******************************************************************
 * 函数名:getVoltageValuesPerData()
 * 功能描述: 每个ADC数值，不同电压档位下对应的电压值
 *          
 * 输入: 电压档位
 * 输出:
 * 返回值:每个ADC数值对应的电压值
 *
 * 修改历史:
 * --------------------
 * 
 * --------------------
 *****************************************************************/
F32 getVoltageValuesPerData(S32 voltScale) {
    //电压各个档位的参量表

    static F32 voltageValuesPerData[]
            = {
        1.0, //1mv
        2.0, //2mv
        5.0, //5mv
        10.0, //10mv
        20.0, //20mv
        50.0, //50mv
        100.0, //100mv
        200.0, //200mv
        500.0, //500mv
        1000.0, //1v
        2000.0, //2v
        5000.0, //5v
        10000.0, //10v
        20000.0, ///20V
    };

    F32 voltagePerData = voltageValuesPerData[voltScale] / ADC_RATIO;
    return voltagePerData;
}


